﻿public class NetHandler_FetchMiniUserInfos : GameServerMessageHandler
{
    public const string Key = "FetchMiniUserInfos";

    public static NetHandler_FetchMiniUserInfos NewHandler()
    {
        return new NetHandler_FetchMiniUserInfos();
    }

    protected override ISerialize NewData() => new FetchMiniUserInfosRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        FetchMiniUserInfosRequestData requestData = _data as FetchMiniUserInfosRequestData;

        GameDebug.Log($"用户{client.userID} 获取MiniUserInfos:{requestData?.userIds.GetArrStr()}");

        UserServer user = Game.instance.GetUser(client.userID);

        FetchMiniUserInfoResponseData response = new FetchMiniUserInfoResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                var uIds = requestData.userIds;

                UserInfo[] res = new UserInfo[uIds.Length];

                for (int i = 0; i < uIds.Length; i++)
                {
                    long deviceId = uIds[i];
                    if (deviceId > 0)
                    {
                        res[i] = UserServer.GetUserInfoWithUserId(deviceId, true);

                        GameDebug.Log($"deviceId:{deviceId}  userInfo:{res[i]}");
                    }
                }

                response.miniUserInfos = res.Map(e=>new MiniUserInfo(e)).AsArray();

                GameDebug.Log($"MiniUserInfos length:{response.miniUserInfos.Length} data:{response.miniUserInfos.GetArrStr()}");

                response.code = 0;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
